import *as Service from "./service";
import {notification} from "antd"
export default {
  namespace:"docker/container",
  state:{
    container:[],
    selectKeys:[],
    imageInfo:[],
    tableLoading:true,
    visibleCreate:false,
    formPortList:[0],
    envList:[]

  },
  subscriptions:{
    setup({dispatch,history}){
      history.listen((location)=>{
        if(location.pathname==='/docker/container'){
          dispatch({type:'query'});
        }
      })
    }
  },
  effects:{
    * query({payload},{call,put}){
      yield put({type:'update',payload:{tableLoading:true}});
      const {data} = yield call(Service.getList);
      yield put({type:'update',payload:{container:data.data}});
      yield put({type:'update',payload:{tableLoading:false}});

    },
    * selectKeys({payload},{call,put}){
      yield put({type:'update',payload:{selectKeys:payload.selectedRowKeys}});
    },
    * stop({payload},{call,put})
    {
      // 停止运行的container
      yield put({type:'update',payload:{tableLoading:true}});
      const {data} = yield call(Service.stop,payload)
      yield put({type:'update',payload:{tableLoading:false}});
      if(data.code===200){
        yield put({type:'query'});
      }
    },
    * start({payload},{call,put})
    {
      // 停止运行的container
      yield put({type:'update',payload:{tableLoading:true}});
      const {data} = yield call(Service.start,payload);
      yield put({type:'update',payload:{tableLoading:false}});
      if(data.code===200){
        yield put({type:'query'});
      }
    },
    * reStart({payload},{call,put})
    {
      // 停止运行的container
      yield put({type:'update',payload:{tableLoading:true}});
      const {data} = yield call(Service.reStart,payload);
      yield put({type:'update',payload:{tableLoading:false}});
      if(data.code===200){
        yield put({type:'query'});
      }
    },
    * remove({payload},{call,put})
    {
      // 移除运行的container
      yield put({type:'update',payload:{tableLoading:true}});
      const {data} = yield call(Service.remove,payload);
      yield put({type:'update',payload:{tableLoading:false}});
      if(data.code===200){
        notification.success({message: '删除成功' ,description:'共删除成功：'+payload.container.length+' 个 container'});
        yield put({type:'update',payload:{selectKeys:[]}});
        yield put({type:'query'});
      }
    },
    * createSubmit({payload},{call,put})
    {
      // 移除运行的container
      yield put({type:'update',payload:{tableLoading:true}});
      const {data} = yield call(Service.create,payload.formData);
      try {
        if(data.code===200){
          yield put({type:'query'});
        }else{
          yield put({type:'update',payload:{tableLoading:false}});
        }
      }catch (e) {
        yield put({type:'update',payload:{tableLoading:false}});
      }

    },
    * updateState({payload},{call,put})
    {
      // 更新state
      yield put({type:'update',payload});
    }
  },
  reducers: {
    update(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  }
}
